package com.visionblue.business.ship.modbus

import com.tencent.bugly.crashreport.CrashReport
import com.visionblue.business.ship.modbus.utilities.floatInverse
import com.visionblue.business.ship.modbus.utilities.longInverse
import com.visionblue.business.ship.model.bean.local.UltraSonicFlowInfo
import com.visionblue.business.ship.util.formatHex4Bits
import java.math.BigDecimal
import kotlin.math.pow

/**
 * @author: xlh
 * @date: 2020-07-31 10:45 AM
 * @desc:
 */
class UltraSonicInfoParser {

    fun parse(results: IntArray): UltraSonicFlowInfo {

        val ultraSonicFlowInfo = UltraSonicFlowInfo()


        // 瞬时流量
        ultraSonicFlowInfo.instantFlowRate = floatInverse(results, 1, 0)

        // 正向累计整数部分
        ultraSonicFlowInfo.positiveAccumulationIntPart = longInverse(results, 9, 8)

        // 正向累计小数部分
        ultraSonicFlowInfo.positiveAccumulationDecimalPart = floatInverse(results, 11, 10)

        // 计算单位  统一转换成立方米


        ultraSonicFlowInfo.positiveAccumulationVal =
            (ultraSonicFlowInfo.positiveAccumulationIntPart + ultraSonicFlowInfo.positiveAccumulationDecimalPart)
//        * 10.0.pow(
//                factor[1] - 3
//            )

//        * convertUnit(factor[0])

//        BigDecimal((ultraSonicFlowInfo.positiveAccumulationIntPart + ultraSonicFlowInfo.positiveAccumulationDecimalPart))
//            .pow()


        return ultraSonicFlowInfo
    }


    private fun convertUnit(oriUnit: Int): Double {
        return when (oriUnit) {
            // 立方米
            // 升
            1 -> 0.001
            // 1美加仑 = 3.785L
            2 -> 0.003785
            // 1英制加仑 = 4.546L
            3 -> 0.004546
            // 1美制兆加仑 =
            // 4 ->
            // 1立方英尺 = 0.0283立方米
            5 -> 0.0283
            // 1美制石油桶 =
            6 -> 0.159
            // 1英制石油桶 =
            7 -> 0.1591
            else -> 1.0
        }
    }
}